﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brain;
using System.IO;

namespace LanguageEditor
{
    public partial class MainForm : Form
    {
        public LanguageFile EdittingFile;

        public MainForm()
        {
            InitializeComponent();

            Engine.InitializeEngineOnlyIO();
        }
        private void CheckFile()
        {
            if (EdittingFile == null)
            {
                EdittingFile = new LanguageFile();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckFile();

            string name = NameUtil.GetUniqueName("Lang");
            this.EdittingFile.Strings.Add(name, "empty");
            this.list_Keys.Items.Add(name);
        }

        private void button_Remove_Click(object sender, EventArgs e)
        {
            CheckFile();
            if (this.list_Keys.SelectedItem != null)
            {
                string key = this.list_Keys.SelectedItem as string;
                this.EdittingFile.Strings.Remove(key);

                this.list_Keys.Items.Remove(this.list_Keys.SelectedItem);
            }
        }


        private void list_Keys_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckFile();
            if (this.list_Keys.SelectedItem != null)
            {
                string key = this.list_Keys.SelectedItem as string;
                string value = this.EdittingFile.Strings[key];
                this.text_Language.Text = value;
                this.text_Key.Text = key;
            }
        }

        private void text_Language_TextChanged(object sender, EventArgs e)
        {
            if (this.list_Keys.SelectedItem != null)
            {
                string key = this.list_Keys.SelectedItem as string;
                if (this.EdittingFile.Strings.ContainsKey(key))
                {
                    string value = this.text_Language.Text;
                    this.EdittingFile.Strings[key] = value;
                }
            }
        }

        private void text_Key_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(text_Key.Text) &&
                this.list_Keys.SelectedItem != null)
            {
                string key = this.list_Keys.SelectedItem as string;
                if (this.EdittingFile.Strings.ContainsKey(key))
                {
                    string value = this.text_Language.Text;

                    this.EdittingFile.Strings.Remove(key);
                    this.EdittingFile.Strings.Add(this.text_Key.Text, value);

                    this.list_Keys.Items[this.list_Keys.Items.IndexOf(key)] = this.text_Key.Text;
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(saveLocation))
            {
                if (this.EdittingFile != null)
                {
                    if (File.Exists(saveLocation))
                    {
                        File.Delete(saveLocation);
                    }
                    using (FileStream stream = new FileStream(saveLocation, FileMode.Create))
                    {
                        Engine.Instance.SerializationManager.SerializeUnique(stream, SerializationType.XML, this.EdittingFile);
                    }
                }
            }
        }

        private string saveLocation;

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.EdittingFile != null)
            {
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "Brain Language Files|*.brainlang";
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string fileName = save.FileName;
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    saveLocation = fileName;
                    using (FileStream stream = new FileStream(fileName, FileMode.Create))
                    {
                        Engine.Instance.SerializationManager.SerializeUnique(stream, SerializationType.XML, this.EdittingFile);
                    }
                }
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Brain Language Files|*.brainlang";
            if (open.ShowDialog() == DialogResult.OK)
            {
                string fileName = open.FileName;

                using (FileStream stream = new FileStream(fileName, FileMode.Open))
                {
                    this.EdittingFile = (LanguageFile)Engine.Instance.SerializationManager.DeserializeUnique(stream, SerializationType.XML);

                    this.list_Keys.Items.Clear();
                    this.text_Key.Text = "";
                    this.text_Language.Text = "";
                    foreach (KeyValuePair<string, string> val in this.EdittingFile.Strings)
                    {
                        this.list_Keys.Items.Add(val.Key);
                    }
                    saveLocation = fileName;
                }
            }
        }
    }
}
